package ch.dreamweb.readpractice.client.lesetraining.train.view;

import ch.dreamweb.readpractice.client.lesetraining.widget.ThreeButtonWidget;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class Unit1ViewImpl extends UnitViewImpl implements Unit1View {

	private static Unit1ViewUiBinder uiBinder = GWT
			.create(Unit1ViewUiBinder.class);

	@UiTemplate("Unit1View.ui.xml")
	interface Unit1ViewUiBinder extends UiBinder<Widget, Unit1ViewImpl> {
	}

	protected String loesch;
	protected int fake;

	protected interface CssStyle extends CssUnitStyle {
		String green();

		String wide();

		String pfuetzenschrift();

		String pfuetzenlabels();
	}

	@UiField
	CssStyle style;

	@UiField
	HorizontalPanel panel01;
	@UiField
	HorizontalPanel panel02;
	@UiField
	HorizontalPanel panel03;
	@UiField
	HorizontalPanel panel04;
	@UiField
	HorizontalPanel panel05;
	@UiField
	HorizontalPanel panel06;
	@UiField
	HorizontalPanel panel07;
	@UiField
	HorizontalPanel panel08;
	@UiField
	HorizontalPanel panel09;
	@UiField
	HorizontalPanel panel10;
	@UiField
	HorizontalPanel panel11;
	@UiField
	HorizontalPanel panel12;
	@UiField
	HorizontalPanel panel13;
	@UiField
	HorizontalPanel panel14;
	@UiField
	HorizontalPanel panel15;
	@UiField
	HorizontalPanel panel16;

	@UiField
	Label label01;
	@UiField
	Label label02;
	@UiField
	Label label03;
	@UiField
	Label label04;
	@UiField
	Label label05;
	@UiField
	Label label06;
	@UiField
	Label label07;
	@UiField
	Label label08;
	@UiField
	Label label09;
	@UiField
	Label label10;
	@UiField
	Label label11;
	@UiField
	Label label12;
	@UiField
	Label label13;
	@UiField
	Label label14;
	@UiField
	Label label15;
	@UiField
	Label label16;
	@UiField
	Button buttonStart;
	@UiField
	TextBox textBoxMorphem;
	@UiField
	Button buttonOk;

	@UiField
	Label labelInfo;

	@UiField
	ThreeButtonWidget three;

	@UiField
	HTML labelInfoNine;

	public Unit1ViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));

		init();
	}

	@Override
	public void init() {
		super.init();

		// Can access @UiField after calling createAndBindUi
		textBoxMorphem.setText("");
		textBoxMorphem.setMaxLength(8);
		textBoxMorphem.setVisibleLength(8);
		textBoxMorphem.setReadOnly(true);
		textBoxMorphem.setTabIndex(-1);
		textBoxMorphem.setVisible(true);
		buttonOk.setText("OK");
		buttonOk.setTabIndex(1);
		buttonOk.setVisible(false);

		// Grafiken laden (Sonst entstehen Lücken bei der Präsentation)
		panel01.addStyleName(style.green());
		panel01.removeStyleName(style.green());
		panel01.addStyleName(style.pfuetzenschrift());
		panel01.removeStyleName(style.pfuetzenschrift());

		three.setVisible(false);

		labelInfoNine.setText("");
	}

	// Add a UI Factory method for the sub-widget & pass object
	@UiFactory
	ThreeButtonWidget makeThreeButtonWidget() {
		Log.debug("enter makeNineButtonWidget");
		return new ThreeButtonWidget();
	}

	@Override
	public void setPresenter(Presenter presenter) {
		super.setPresenter(presenter);
		three.setEventBus(presenter.getEventBus());
	}

	@UiHandler("buttonStart")
	void onButtonStartClick(ClickEvent e) {
		if (presenter != null) {
			presenter.onStartButtonClicked();
		}
	}

	@UiHandler("buttonOk")
	void onButtonOkClick(ClickEvent e) {
		if (presenter != null) {
			presenter.onOkButtonClicked();
		}

	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void setMorphem(String morphem) {
		textBoxMorphem.setText(morphem);
	}

	@Override
	public void setOkEnabled(boolean enabled) {
		buttonOk.setEnabled(enabled);
		buttonOk.setFocus(true);
	}

	@Override
	public void setStartEnabled(boolean enabled) {
		buttonStart.setEnabled(enabled);
	}

	@Override
	public void setFocusStart() {
		buttonStart.setFocus(true);
	}

	@Override
	public void setThree(String correct, String wrongOne, String wrongTwo) {
		three.setTexts(correct, wrongOne, wrongTwo);
		three.setEnabled(true);
		three.setVisible(true);
		three.setFocus();
	}

	@Override
	public void setThreeVisible(boolean visible) {
		three.setVisible(visible);
	}

	@Override
	public void setThreeEnabled(boolean enabled) {
		three.setEnabled(enabled);
	}

	@Override
	public void setInfoNine(String text) {
		labelInfoNine.setText(text);
	}

	@Override
	public void addInfoNineCorrect(String correct) {
		labelInfoNine.setHTML(labelInfoNine.getHTML() + "&nbsp;<span class=\""
				+ style.correct() + "\">" + correct + "</span>");
	}

	@Override
	public void addInfoNineIncorrect(String wrong, String correct) {
		labelInfoNine.setHTML(labelInfoNine.getHTML() + "&nbsp;<span class=\""
				+ style.incorrect() + " " + style.stroke() + "\">" + wrong
				+ "</span>&nbsp;<span class=\"" + style.correct() + "\">"
				+ correct + "</span>");
	}

	@Override
	public void changeColor(int buttonNumber) {
		switch (buttonNumber) {
		case 0: {
			panel01.addStyleName(style.green());
			break;
		}
		case 1: {
			panel02.addStyleName(style.green());

			break;
		}
		case 2: {
			panel03.addStyleName(style.green());
			break;
		}
		case 3: {
			panel04.addStyleName(style.green());
			break;
		}
		case 4: {
			panel05.addStyleName(style.green());
			break;
		}
		case 5: {
			panel06.addStyleName(style.green());
			break;
		}
		case 6: {
			panel07.addStyleName(style.green());
			break;
		}
		case 7: {
			panel08.addStyleName(style.green());
			break;
		}
		case 8: {
			panel09.addStyleName(style.green());
			break;
		}
		case 9: {
			panel10.addStyleName(style.green());
			break;
		}
		case 10: {
			panel11.addStyleName(style.green());
			break;
		}
		case 11: {
			panel12.addStyleName(style.green());
			break;
		}
		case 12: {
			panel13.addStyleName(style.green());
			break;
		}
		case 13: {
			panel14.addStyleName(style.green());
			break;
		}
		case 14: {
			panel15.addStyleName(style.green());
			break;
		}
		case 15: {
			panel16.addStyleName(style.green());
			break;
		}

		}
	}

	@Override
	public void resetColor() {
		panel01.removeStyleName(style.green());
		panel02.removeStyleName(style.green());
		panel03.removeStyleName(style.green());
		panel04.removeStyleName(style.green());
		panel05.removeStyleName(style.green());
		panel06.removeStyleName(style.green());
		panel07.removeStyleName(style.green());
		panel08.removeStyleName(style.green());
		panel09.removeStyleName(style.green());
		panel10.removeStyleName(style.green());
		panel11.removeStyleName(style.green());
		panel12.removeStyleName(style.green());
		panel13.removeStyleName(style.green());
		panel14.removeStyleName(style.green());
		panel15.removeStyleName(style.green());
		panel16.removeStyleName(style.green());
	}

	@Override
	public void setButtons(String[] all) {
		label01.setText(all[0]);
		label02.setText(all[1]);
		label03.setText(all[2]);
		label04.setText(all[3]);
		label05.setText(all[4]);
		label06.setText(all[5]);
		label07.setText(all[6]);
		label08.setText(all[7]);
		label09.setText(all[8]);
		label10.setText(all[9]);
		label11.setText(all[10]);
		label12.setText(all[11]);
		label13.setText(all[12]);
		label14.setText(all[13]);
		label15.setText(all[14]);
		label16.setText(all[15]);
		if (all[0].equals("")) {
			panel01.removeStyleName(style.pfuetzenschrift());
			panel02.removeStyleName(style.pfuetzenschrift());
			panel03.removeStyleName(style.pfuetzenschrift());
			panel04.removeStyleName(style.pfuetzenschrift());
			panel05.removeStyleName(style.pfuetzenschrift());
			panel06.removeStyleName(style.pfuetzenschrift());
			panel07.removeStyleName(style.pfuetzenschrift());
			panel08.removeStyleName(style.pfuetzenschrift());
			panel09.removeStyleName(style.pfuetzenschrift());
			panel10.removeStyleName(style.pfuetzenschrift());
			panel11.removeStyleName(style.pfuetzenschrift());
			panel12.removeStyleName(style.pfuetzenschrift());
			panel13.removeStyleName(style.pfuetzenschrift());
			panel14.removeStyleName(style.pfuetzenschrift());
			panel15.removeStyleName(style.pfuetzenschrift());
			panel16.removeStyleName(style.pfuetzenschrift());
		} else {
			panel01.addStyleName(style.pfuetzenschrift());
			panel02.addStyleName(style.pfuetzenschrift());
			panel03.addStyleName(style.pfuetzenschrift());
			panel04.addStyleName(style.pfuetzenschrift());
			panel05.addStyleName(style.pfuetzenschrift());
			panel06.addStyleName(style.pfuetzenschrift());
			panel07.addStyleName(style.pfuetzenschrift());
			panel08.addStyleName(style.pfuetzenschrift());
			panel09.addStyleName(style.pfuetzenschrift());
			panel10.addStyleName(style.pfuetzenschrift());
			panel11.addStyleName(style.pfuetzenschrift());
			panel12.addStyleName(style.pfuetzenschrift());
			panel13.addStyleName(style.pfuetzenschrift());
			panel14.addStyleName(style.pfuetzenschrift());
			panel15.addStyleName(style.pfuetzenschrift());
			panel16.addStyleName(style.pfuetzenschrift());
		}
	}

	@Override
	public Label getLabelInfo() {
		return labelInfo;
	}

	@Override
	public CssUnitStyle getStyle() {
		return style;
	}

	@Override
	public Button getButtonStart() {
		return buttonStart;
	}
}
